#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"

/* widgets:
Layer_PTLight
Container_CloseOrNot
PopupButton_CloseOrNot_Close
PopupButton_CloseOrNot_Cancel
Container_OpenOrNot
PopupButton_OpenOrNot_Open
PopupButton_OpenOrNot_Cancel
Background_PTLight
Container_PTLight_On
Text_PTLight_ON
PopupButton_PTLight_OFF
Icon_PTLight_JindutiaoOn
Container_PTLight_Off
Text_PTLight_OFF
PopupButton_PTLight_ON
Icon_PTLight_JindutiaoOff
Button_Back_MainMenu
*/
static ITUContainer* Container_PTLight_Off;
static ITUContainer* Container_PTLight_On;
bool PTLightOnEnter(ITUWidget* widget, char* param)
{
	if (!Container_PTLight_On)
	{
		Container_PTLight_On = ituSceneFindWidget(&theScene, "Container_PTLight_On");
		assert(Container_PTLight_On);
		Container_PTLight_Off = ituSceneFindWidget(&theScene, "Container_PTLight_Off");
		assert(Container_PTLight_On);
	}
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
	{
		ituWidgetSetVisible(Container_PTLight_On, true);
		ituWidgetSetVisible(Container_PTLight_Off, false);
	}
	else
	{
		ituWidgetSetVisible(Container_PTLight_On, false);
		ituWidgetSetVisible(Container_PTLight_Off, true);
	}
    return true;
}

bool PTLightOnLeave(ITUWidget* widget, char* param)
{
    return true;
}
bool PTLightOnTimer(ITUWidget* widget, char* param)
{

	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}

	//设备状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
		{
			ituWidgetSetVisible(Container_PTLight_On, true);
			ituWidgetSetVisible(Container_PTLight_Off, false);
		}
		else
		{
			ituWidgetSetVisible(Container_PTLight_On, false);
			ituWidgetSetVisible(Container_PTLight_Off, true);
		}
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	return true;
}
bool PopupButton_PTLight_OFF_Press(ITUWidget* widget, char* param)
{
	ituWidgetSetVisible(Container_PTLight_On, false);
	ituWidgetSetVisible(Container_PTLight_Off, true);
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0;//状态关
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 1;//命令关
	printf("Moorgen INFO : Touch to turn off the PT_Light\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

bool PopupButton_PTLight_ON_Press(ITUWidget* widget, char* param)
{
	ituWidgetSetVisible(Container_PTLight_On, true);
	ituWidgetSetVisible(Container_PTLight_Off, false);
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 1;//状态开
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 2;//命令开
	printf("Moorgen INFO : Touch to turn on the PT_Light\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

